home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / SQUARANT / SQUARANT.C next >
Text File  |  1990-11-15  |  10KB  |  374 lines

  1. /***********************************************************************************
  2.  
  3.     SIMPLE SYSTEMS¬ CLASS LIBRARY
  4.     
  5.         SQuarantine.c
  6.             
  7.             This is a class for virus protection.  This object scans each
  8.             resource in the given resource file and compares the resource
  9.             profile against a developer defined table, a resource created
  10. via a ResEdit tem            plate.  See the header file, SQuarantine.h,
  11. for details.
  12.             
  13.             READ THE SQUARANTINE ('Qnte') RESOURCE FORMAT NOTES - UNLESS
  14.             THE LAST RECORD IN THE 'Qnte' RESOURCE HAS A SIZE VALUE OF -1, 
  15.             THE QUARANTINE METHODS WILL BE IN AN ENDLESS LOOP, ATTEMPTING
  16.             TO SCAN BEYOND THE QUARANTINE MAP IN MEMORY.
  17.             
  18.             This source code is released for limited use, any commerical
  19.             product that uses this source code should acknowledge the author,
  20.             Schorschi LeRoy Decker, in the manual documentation and/or in the
  21.             'About...' window for his creative effort.
  22.             
  23.         Version 1.0.0  11/10/90  
  24.         Schorschi LeRoy Decker
  25.         
  26.             Made some documentation changes.
  27.             
  28.         Version 1.0.1  11/15/90
  29.         Schorschi LeRoy Decker
  30.         
  31.  ***********************************************************************************
  32.  
  33.     IN NO WAY, SHAPE, OR FORM IS THIS FILE RELEASED TO BUSINESS ENTITIES THAT
  34.     CHARGE A FEE FOR DISTRIBUTION.  FIRMS LIKE EDUCORP¬, WHICH CHARGE DISTRIBUTION
  35.     FEES FOR PUBLIC DOMAIN OR SHAREWARE PRODUCTS, MUST RECEIVE WRITTEN PERMISSION
  36.     FROM SIMPLE SYSTEMS¬ AND THE ORIGINAL AUTHOR OF THIS SOURCE CODE.  BULLETIN BOARD
  37.     SYSTEMS AND ON-LINE SERVICES THAT CHARGE A CONNECT FEE ARE GIVEN LIMITED
  38.     PERMISSION FOR DOWNLOAD ACCESS BY MEMBERS.  ANY VIOLATION OF THIS RESERVED RIGHT 
  39.     OF LIMITED DISTRIBUTION WILL BE INFORCED TO THE FULLEST EXTENT OF THE LAW.
  40.     
  41.     COPYRIGHT ⌐ 1990 SIMPLE SYSTEMS¬.  ALL RIGHTS ARE RESERVED WORLD-WIDE.
  42.     
  43.         SIMPLE SYSTEMS¬ is the trademark and property of SIMPLE SYSTEMS CONSULTING.
  44.         EDUCORP¬ is the trademark and property of EDUCORP Inc.
  45.             
  46.  ***********************************************************************************
  47.         
  48.     Questions, comments, etc.
  49.     
  50.         To: SIMPLE SYSTEMS
  51.             Schorschi LeRoy Decker
  52.             5067 Golden Ave.
  53.             Riverside, CA  92505
  54.  
  55.         America-On-Line:    Schorschi
  56.         CompuServe:            73020,546
  57.         
  58.         Phone:    (714) 597-1234    Please not after 11:00 pm (Pacific) or before
  59.                                 7:00 am (Pacific).
  60.  
  61.  ***********************************************************************************/
  62.  
  63. #include <Global.h>
  64. #include <CError.h>
  65.  
  66. #include "SQuarantine.h"
  67.  
  68. /***********************************************************************************
  69.  
  70.     IQuarantine()
  71.     
  72.         Initialize quarantine object.
  73.  
  74.  ***********************************************************************************/
  75.  
  76. void SQuarantine::IQuarantine(int theResourceFile)
  77.     {
  78.         int theCurrentResourceFile;
  79.  
  80.  
  81.         theCurrentResourceFile = CurResFile();
  82.         if (theResourceFile != theCurrentResourceFile)
  83.             UseResFile(theResourceFile);
  84.         
  85.         theQuarantineMap = (StringHandle)Get1Resource(rQuarantineType, 
  86.                                rQuarantineNumber);
  87.         theQuarantineFile = theResourceFile;
  88.                    
  89.         UseResFile(theCurrentResourceFile);
  90.         CheckResource((Handle)theQuarantineMap);
  91.     }
  92.  
  93. /***********************************************************************************
  94.  
  95.     Dispose()
  96.     
  97.         Dispose of the quarantine object.
  98.  
  99.  ***********************************************************************************/
  100.  
  101. void SQuarantine::Dispose()
  102.     {
  103.         if (theQuarantineMap != NULL)
  104.             ReleaseResource((Handle)theQuarantineMap);
  105.             
  106.         inherited::Dispose();
  107.     }
  108.  
  109. /***********************************************************************************
  110.  
  111.     Scan()
  112.     
  113.         Loads each resource in the given resource file, and verify resource
  114.         according to the given flag values.
  115.  
  116. ************************************************************************************/
  117.  
  118. Boolean SQuarantine::Scan(short theFlags)
  119.     {
  120.         int        theCurrentResourceFile, theTypes, theTypeIndex,
  121.                 theResources,theResourcesIndex;
  122.         long    theType;
  123.         Handle    theResource;
  124.         Boolean    theResult;
  125.         
  126.         
  127.         theResult = FALSE;
  128.         if ((theQuarantineMap != NULL) && (theQuarantineFile != -1))
  129.             {
  130.                 theCurrentResourceFile = CurResFile();
  131.                 if (theQuarantineFile != theCurrentResourceFile)
  132.                     UseResFile(theQuarantineFile);
  133.                 
  134.                 theResult = TRUE;
  135.                 
  136.                 theTypeIndex = 0;
  137.                 theTypes = Count1Types();
  138.                 
  139.                 while (((++theTypeIndex) <= theTypes) && theResult)
  140.                     {
  141.                         Get1IndType(&theType, theTypeIndex);
  142.                         
  143.                         theResourcesIndex = 0;
  144.                         theResources = Count1Resources(theType);
  145.                         
  146.                         while (((++theResourcesIndex) <= theResources) && theResult)
  147.                             {
  148.                                 theResource = Get1IndResource(theType, theResourcesIndex);
  149.                                 CheckResource(theResource);
  150.                                 
  151.                                 if (theFlags % kTypeAndID)
  152.                                     {
  153.                                         theResult = TypeAndID(theResource);
  154.                                         if ((theFlags & kName) && theResult)
  155.                                             {
  156.                                                 theResult = Name(theResource);
  157.                                                 if ((theFlags & kSize) && theResult)
  158.                                                     theResult = Size(theResource);
  159.                                             }
  160.                                     }
  161.                             }
  162.                     }
  163.                 
  164.                 UseResFile(theCurrentResourceFile);
  165.                 
  166.                 if (theTypeIndex <= theTypes) 
  167.                     theResult = FALSE;
  168.             }
  169.             
  170.         return (theResult);
  171.     }
  172.  
  173. /***********************************************************************************
  174.  
  175.     TypeAndID()
  176.     
  177.         Scan the quarantine table for a match to the given type.  Should a
  178.         match be found then the found field for the match is incremented by 
  179.         the type flag.
  180.  
  181.  ***********************************************************************************/
  182.  
  183. Boolean SQuarantine::TypeAndID(Handle theResource)
  184.     {
  185.         QuarantinePtr    thePointer;
  186.         long            theType;
  187.         int                theID;
  188.         Str255            theName;
  189.         Boolean            theResult;
  190.         
  191.         
  192.         theResult = FALSE;
  193.         if ((theQuarantineMap != NULL) && (theResource != NULL) && 
  194.             (theQuarantineFile != -1))
  195.             {
  196.                 GetResInfo(theResource, &theID, &theType, &theName);
  197.                 
  198.                 HLock(theQuarantineMap);
  199.                 thePointer = (QuarantinePtr)(*theQuarantineMap);
  200.                 
  201.                 while (thePointer->size != -1)
  202.                     {
  203.                         if ((thePointer->type == theType) &&
  204.                             (thePointer->id == theID))
  205.                             {
  206.                                 thePointer->flags += kTypeAndID;
  207.                                 theResult = TRUE;
  208.                                 
  209.                                 break;
  210.                             }
  211.                         
  212.                         thePointer = (QuarantinePtr)((StringPtr)thePointer + 
  213.                                          (kQuarantineShift + thePointer->name[0] + 1));                        
  214.                     }
  215.  
  216.                 
  217.                 HUnlock(theQuarantineMap);
  218.             }
  219.             
  220.         return (theResult);
  221.     }
  222.  
  223. /***********************************************************************************
  224.  
  225.     Name()
  226.     
  227.         Scan the quarantine table for a match to the given type, id, and
  228.         name.  Should a match be found then the found field for the match
  229.         is incremented by the ID flag.
  230.     
  231. ***********************************************************************************/
  232.  
  233. Boolean SQuarantine::Name(Handle theResource)
  234.     {
  235.         QuarantinePtr    thePointer;
  236.         long            theType;
  237.         int                theID;
  238.         Str255            theName;
  239.         Boolean            theResult;
  240.         
  241.         
  242.         theResult = FALSE;
  243.         if ((theQuarantineMap != NULL) && (theResource != NULL) && 
  244.             (theQuarantineFile != -1))
  245.             {
  246.                 GetResInfo(theResource, &theID, &theType, &theName);
  247.                 
  248.                 HLock(theQuarantineMap);
  249.                 thePointer = (QuarantinePtr)(*theQuarantineMap);
  250.                 
  251.                 while (thePointer->size != -1)
  252.                     {
  253.                         if ((thePointer->type == theType) && 
  254.                             (thePointer->id == theID) &&
  255.                             (EqualString(thePointer->name, theName, TRUE, TRUE)))
  256.                             {
  257.                                 thePointer->flags += kName;
  258.                                 theResult = TRUE;
  259.                                 
  260.                                 break;
  261.                             }
  262.                         
  263.                         thePointer = (QuarantinePtr)((StringPtr)thePointer + 
  264.                                          (kQuarantineShift + thePointer->name[0] + 1));                        
  265.                     }
  266.                 
  267.                 HUnlock(theQuarantineMap);
  268.             }
  269.             
  270.         return (theResult);
  271.     }
  272.  
  273. /***********************************************************************************
  274.  
  275.     Size()
  276.     
  277.         Scan the quarantine table for a match to the given type, id, name, and
  278.         size.  Should a match be found then the found field for the match is
  279.         incremented by the size flag.
  280.  
  281.  ***********************************************************************************/
  282.  
  283. Boolean SQuarantine::Size(Handle theResource)
  284.     {
  285.         QuarantinePtr    thePointer;
  286.         long            theType, theSize;
  287.         int                theID;
  288.         Str255            theName;
  289.         Boolean            theResult;
  290.         
  291.         
  292.         theResult = FALSE;
  293.         if ((theQuarantineMap != NULL) && (theResource != NULL) && 
  294.             (theQuarantineFile != -1))
  295.             {
  296.                 GetResInfo(theResource, &theID, &theType, &theName);
  297.                 theSize = SizeResource(theResource);
  298.                 
  299.                 HLock(theQuarantineMap);
  300.                 thePointer = (QuarantinePtr)(*theQuarantineMap);
  301.                 
  302.                 while (thePointer->size != -1)
  303.                     {
  304.                         if ((thePointer->type == theType) && 
  305.                             (thePointer->id == theID) &&
  306.                             (EqualString(thePointer->name, theName, TRUE, TRUE)) &&
  307.                             (thePointer->size == theSize))
  308.                             {
  309.                                 thePointer->flags += kSize;
  310.                                 theResult = TRUE;
  311.                                 
  312.                                 break;
  313.                             }
  314.                         
  315.                         thePointer = (QuarantinePtr)((StringPtr)thePointer + 
  316.                                          (kQuarantineShift + thePointer->name[0] + 1));                        
  317.                     }
  318.                 
  319.                 HUnlock(theQuarantineMap);
  320.             }
  321.             
  322.         return (theResult);
  323.     }
  324.  
  325. /***********************************************************************************
  326.  
  327.     Missing()
  328.     
  329.         Scan the quarantine table for resources that were not verified for type,
  330.         id, name, or size, based on the given flags value.  This method should
  331.         be called after calling the Scan() method to determine if any resources
  332.         that were expected to be found are missing.
  333.  
  334.  ***********************************************************************************/
  335.  
  336. Boolean SQuarantine::Missed()
  337.     {
  338.         QuarantinePtr    thePointer;
  339.         long            theType, theSize;
  340.         int                theID;
  341.         Str255            theName;
  342.         Boolean            theResult;
  343.         
  344.         
  345.         theResult = FALSE;
  346.         if ((theQuarantineMap != NULL) && (theQuarantineFile != -1))
  347.             {
  348.                 HLock(theQuarantineMap);
  349.                 thePointer = (QuarantinePtr)(*theQuarantineMap);
  350.                 
  351.                 while (thePointer->size != -1)
  352.                     {
  353.                         if (thePointer->flags == 0)
  354.                             {
  355.                                 theResult = TRUE;
  356.                                 break;
  357.                             }
  358.                         
  359.                         thePointer = (QuarantinePtr)((StringPtr)thePointer + 
  360.                                          (kQuarantineShift + thePointer->name[0] + 1));                        
  361.                     }
  362.                 
  363.                 HUnlock(theQuarantineMap);
  364.             }
  365.             
  366.         return (theResult);
  367.     }
  368.  
  369. /***********************************************************************************
  370.  
  371.     End of File - SQuarantine.c
  372.     
  373.  ***********************************************************************************/
  374.